package com.cmrx.model.controller;


import com.cmrx.model.entity.*;
import com.cmrx.model.service.InvestigationService;
import com.cmrx.model.service.LoginService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.vo.ResultVo;
import com.github.pagehelper.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("case")
@Slf4j
@Api(value = "/case",description = "案件详情接口类")
public class InvestigationController {
	@Autowired
	private InvestigationService investigationService;
	@Autowired
	private LoginService loginService;

	@RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "案件列表或详情接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\",\"investigationid\":\"现勘id\"}")
	public ResultVo selectInvestigationList(@RequestBody Map<String,String> map, HttpServletRequest request) {
			String investigationid = map.get("investigationid");
			String token = map.get("token");
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
		    User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
		    String userid = user.getUserid();
			if(StringUtils.isEmpty(userid)){
				return ResultVoUtil.message("用户id为空");
			}
        return ResultVoUtil.success(investigationService.selectListLawCase(userid, investigationid));
	}

	@RequestMapping(value = "/add/case",method = RequestMethod.POST)
    @ApiOperation(value = "新增案件接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\"}")
	public ResultVo addCase(@RequestBody Map<String,String> map) {
			String token = map.get("token");
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
			User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
			String userid =user.getUserid();
			String username = user.getUserName();
			if(!StringUtils.isEmpty(userid)) {
				return ResultVoUtil.success(investigationService.addCase(userid, username,user.getOrganName(),user.getTechid(),user.getUserCard()));
			}
			return ResultVoUtil.message("用户id为空");
	}

    @RequestMapping(value = "/delete/case",method = RequestMethod.POST)
    @ApiOperation(value = "删除案件接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\",\"investigationid\":\"现勘id\"},\"caseid\":\"案件id\"}")
	public ResultVo deleteCase(@RequestBody Map<String,String> map) {
			String token = map.get("token");
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
			User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
			String investigationid = map.get("investigationid");
			String caseid = map.get("caseid");
			if(StringUtils.isEmpty(investigationid)) {
				return ResultVoUtil.message("investigationid为空");
			}
			if(StringUtils.isEmpty(caseid)) {
				return ResultVoUtil.message("caseid为空");
			}
			investigationService.deleteCase(investigationid, caseid);
		    return ResultVoUtil.success(null);
	}
	
	@RequestMapping(value = "/update/case",method = RequestMethod.POST)
    @ApiOperation(value = "修改案件接口")
	public ResultVo updateCase(@RequestBody CaseDetail caseDetail) {
			String token = caseDetail.getToken();
			User user = JwtUtil.deToken(token);
			caseDetail.setCreate_user(user.getUserName());
			if(!StringUtils.isEmpty(caseDetail.getInvestigationid())&&StringUtil.isNotEmpty(caseDetail.getCaseid())) {
				return ResultVoUtil.success(investigationService.updateCase(caseDetail));
			}
			return ResultVoUtil.message("investigationid或caseid为空");
	}
	
	@RequestMapping(value = "/addorupdate/witness",method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改见证人接口")
	public ResultVo addWitness(@RequestBody Witness witness) {
			String token = witness.getToken();
			User user = JwtUtil.deToken(token);
			witness.setCreateUser(user.getUserName());
			if(!StringUtils.isEmpty(witness.getInvestigationId())) {
				return ResultVoUtil.success(investigationService.addOrUpdateWitness(witness));
			}
			return ResultVoUtil.message("investigationid为空");
	}

	@RequestMapping(value = "/delete/witness",method = RequestMethod.POST)
    @ApiOperation(value = "删除见证人接口")
	public ResultVo deleteWitness(@RequestBody Witness witness) {
			String token = witness.getToken();
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
			User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
			investigationService.deleteWitness(witness);
			return ResultVoUtil.success(null);
	}


	@RequestMapping(value = "/addorupdate/lostgoods",method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改损失物品接口")
	public ResultVo addLostgoods(@RequestBody LostGoods lostGoods) {
			String token = lostGoods.getToken();
			User user = JwtUtil.deToken(token);
		    lostGoods.setCreate_user(user.getUserName());
			if(!StringUtils.isEmpty(lostGoods.getInvestigation_id())) {
				return ResultVoUtil.success(investigationService.addOrUpdateLostgoods(lostGoods,user));
			}
			return ResultVoUtil.message("investigationid为空");
	}

	@RequestMapping(value = "/delete/lostgoods",method = RequestMethod.POST)
    @ApiOperation(value = "删除损失物品接口")
	public ResultVo deleteLostgoods(@RequestBody LostGoods lostGoods) {
			String token = lostGoods.getToken();
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
			User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
			investigationService.deleteLostgoods(lostGoods);
		return ResultVoUtil.success(null);
	}
	
	@RequestMapping(value = "/addorupdate/victim",method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改受害人报案人接口")
	public ResultVo addVictim(@RequestBody Victim victim) {
			String token = victim.getToken();
			User user = JwtUtil.deToken(token);
			victim.setCreateUser(user.getUserName());
			if(!StringUtil.isEmpty(victim.getCaseId())) {
				return ResultVoUtil.success(investigationService.addOrUpdateVictim(victim,user));
			}
			return ResultVoUtil.message("caseId为空");
	}
	
	@RequestMapping(value = "/delete/victim",method = RequestMethod.POST)
    @ApiOperation(value = "删除受害人接口")
	public ResultVo deleteVictim(@RequestBody Victim victim) {
			String token = victim.getToken();
			if(StringUtils.isEmpty(token)){
				return ResultVoUtil.message("token为空");
			}
			User user = JwtUtil.deToken(token);
			if(user==null){
				return ResultVoUtil.message("用户信息为空");
			}
			investigationService.deleteVictim(victim);
		    return ResultVoUtil.success(null);
	}

	@RequestMapping(value = "/submit/case",method = RequestMethod.POST)
    @ApiOperation(value = "提交案件接口")
    @ApiImplicitParam(name = "map",value = "{\"investigationid\":\"\"}")
	public ResultVo addVictim(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.message("token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.message("用户信息为空");
        }
		String investigationid = map.get("investigationid");
		if(StringUtils.isEmpty(investigationid)){
			return ResultVoUtil.message("investigationid为空");
		}
		List<CaseDetail> list =  investigationService.selectListLawCase("",investigationid);
		if(!list.isEmpty()){
			return ResultVoUtil.success(investigationService.submitCase(list.get(0),user));
		}
		return ResultVoUtil.success(null);
	}

	@RequestMapping(value = "/select/user",method = RequestMethod.POST)
    @ApiOperation(value = "获取当前人员单位人员接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\"}")
	public ResultVo selectUser(@RequestBody Map<String,String> map) {
		String token = map.get("token");
		if(StringUtils.isEmpty(token)){
			return ResultVoUtil.message("token为空");
		}
		User user = JwtUtil.deToken(token);
		if(user==null){
			return ResultVoUtil.message("用户信息为空");
		}
		return ResultVoUtil.success(investigationService.selectUserByRegionalism(user.getRegionalism()));
	}

	@RequestMapping(value = "/create/casecontent",method = RequestMethod.POST)
    @ApiOperation(value = "生成案件发现过程模板接口")
    @ApiImplicitParam(name = "map",value = "{\"victims\":\"\",\"case_type\":\"\",\"case_type_name\":\"\",\"occurrence_date_from\":\"\",\"scene_detail\":\"\"}")
	public ResultVo createCaseContent(@RequestBody Map<String,String> map) {
		String victims = map.get("victims");
		if(StringUtils.isEmpty(victims)){
			return ResultVoUtil.message("受害人报案人为空");
		}
		String case_type = map.get("case_type");
		if(StringUtils.isEmpty(case_type)){
			return ResultVoUtil.message("案件类别码为空");
		}
		String case_type_name = map.get("case_type_name");
		if(StringUtils.isEmpty(case_type_name)){
			return ResultVoUtil.message("案件类别名称为空");
		}
		//发案时间开始
		String occurrence_date_from = map.get("occurrence_date_from");
		if(StringUtils.isEmpty(occurrence_date_from)){
			return ResultVoUtil.message("发案时间为空");
		}
		String scene_detail = map.get("scene_detail");
		if(StringUtils.isEmpty(scene_detail)){
			return ResultVoUtil.message("发案地点为空");
		}
		//盗三车 '050213','050228','050224'
		String result = "";
		String[] dsc ={"050213","050228","050224"};
		if(Arrays.asList(dsc).contains(case_type)){
			// 据%s报警称，%s发现停放在发案地点的***被盗。
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "发现停放在" + scene_detail + "的***被盗";
			return ResultVoUtil.success(result);
		}
		//扒窃拎包 '05021706','05021705','05021704','05021703','05021702','05021701'
		String[] bqlb = {"05021706","05021705","05021704","05021703","05021702","05021701"};
		if(Arrays.asList(bqlb).contains(case_type)){
			//  据%s报警称，%s在%s发现***被窃。
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "在"+scene_detail+"发现***被窃";
			return ResultVoUtil.success(result);
		}
		//露天空间 '050220','050234','050205'
		String[] ltkj ={"050220","050234","050205"};
		if(Arrays.asList(ltkj).contains(case_type)){
			//  据%s报警称，%s在%s发现***被窃。
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "在"+scene_detail+"发现***被窃";
			return ResultVoUtil.success(result);
		}
		//街面诈骗 '050310','050320','050308','050315','050319','050318','050327'
		String[] jmzp ={"050310","050320","050308","050315","050319","050318","050327"};
		if(Arrays.asList(jmzp).contains(case_type)){
			//   据%s报警称，%s在%s发生了一起%s
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "在"+scene_detail+"发生了一起"+case_type_name;
			return ResultVoUtil.success(result);
		}
		//伤害斗殴
		String[] shdo ={"060136","060137","04010310","04010309","04010308","04010307","04010306","04010305","04010304","04010303","04010302","04010301"};
		if(Arrays.asList(shdo).contains(case_type)){
			//   据%s报警称，%s在%s发生了一起%s。
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "在"+scene_detail+"发生了一起"+case_type_name;
			return ResultVoUtil.success(result);
		}
		//破坏财物 '050902','050901'
		String[] phcw ={"050902","050901"};
		if(Arrays.asList(phcw).contains(case_type)){
			//   //据%s报警称，%s在%s发现***被破坏。
			result = "据"+ victims +"报警称，"+ occurrence_date_from + "在"+scene_detail+"发现***被破坏";
			return ResultVoUtil.success(result);
		}
		return ResultVoUtil.success(result);
	}


}
